<?php
namespace User\Controller;
use Think\Controller;
use Think\Hook;

/**
 *  分享文章
 */
class ArticleController extends CkController {
    
    function _initialize()
    {
      //\Think\Hook::add('view_end','User\Behaviors\testBehavior');
      //\Think\Hook::add('view_end','User\Behaviors\JiFeiBehavior');
    }
 
    //文章列表
    public function index()
    {
         $url_uid= I('get.uid');
         $data['cid']= I('get.cid')?I('get.cid'):0;   
         $data['uid']=session("user.id");
         $uid=de_crypt($data['uid']);
         if($uid){
            $data['user']=M('user')->where('id='.$uid)->find();
         }
         $data['cate']=M("Cate")->field("id,name,look")->where("look=0")->order("sort asc,id asc")->select();
         $p=I('get.p')?(int)I('get.p'):0;//当前页数
         $cid=I('get.cid')?(int)I('get.cid'):'';
         $this->assign($data);
         $this->display();
    }


    //AJax
    public function ajax()
    {
        $cid=I('get.cid');
        $page=I('get.page') ? I('get.page') : 0;
        $newslist=D("NewsView");
        $where=array("News.look"=>0,); //调用没有锁定文章
        if($cid){$where["News.cid"]=$cid;} //判断有没有选择分类id
        $count=$newslist->where($where)->count();//计算总数
        $page_size=C("FENYE_SIZE");
        $page_total_num = ceil($count/$page_size);
        $page_start = $page * $page_size;
        $data=array(
            "total_num" => $count,
            "page_size" => $page_size,
            "page_total_num" => $page_total_num,
        );
        $data['newslist']=$newslist->where($where)->limit($page_start,$page_size)->order("News.hot desc,News.sort asc,News.id desc")->select();
        foreach ($data['newslist'] as $key=>$v) {
			$data['newslist'][$key]['click']= $v['click'] + $v['pv'];
            $data['newslist'][$key]['url']=show_url2($v);
	    $data['newslist'][$key]['litpic']=$v['litpic'];
            unset($data['newslist'][$key]['content']);
            if($data['newslist'][$key]['price']<= 0){
               if($data['newslist'][$key]['c_price']<=0){
                 $price=C("QUJU_DIANJI_PRICE");
               }else{
                 $price=$data['newslist'][$key]['c_price'];
               }
               $data['newslist'][$key]['price']=$price;
            }
            unset($data['newslist'][$key]['c_price']);
            if(!session("user")){
               $data['newslist'][$key]['price']=0;
            }
            if(session("user")){
               if($data['newslist'][$key]['item_id']){
                 $suk= M("shop_item")->field("suk")->where("id=".$data['newslist'][$key]['item_id'])->find();
                 $suk= json_decode($suk['suk'],true);
                 $suk2['min']=min(ticheng_array($suk));
                 $suk2['max']= max(ticheng_array($suk));
                 $data['newslist'][$key]['suk']=$suk2;
               }
            }
        }
        $this->ajaxReturn($data);
    }
    
    
    //检查是否绑定微信
    public function weixin()
    { 
     $this->chak_login();
      if(C("USER_REG")){
         $data['uid']=session("user.id");
         $uid=de_crypt($data['uid']);
         $user = M("user")->field("open_id,nickname,look,user_tel")->where("id=".$uid)->find();
         if(empty($user['open_id']) || empty($user['nickname']) || $user['look']==0){
            $this->display();
         }else{
           if(C("WEIXIN_REG_TEL")){ 
            if(empty($user['user_tel'])){
             $this->display("tel");
            }else{
                $this->redirect(U("/Article/index/".session('user.id')."?cid=".C("CATE_ID")));               
            }
            }else{
                $this->redirect(U("/Article/index/".session('user.id')."?cid=".C("CATE_ID")));
            }
         }
        }else{
            $this->redirect(U("/Article/index/".session('user.id')."?cid=".C("CATE_ID")));            
       }
    }
    

    //补充手机账号和密码
    public function buchong_tel()
    {   
        $this->chak_login();
        $data=I("post.");
        $user = D("user"); // 实例化User对象 // 手动进行令牌验证 
        if (!$user->create()){
            $this->error($user->getError());
        }
        if(C("MSG_STATE")){
         if(empty($data['code'])){
            $this->error("验证码必须填写!");
         }
         if($data['code']!==cookie("code") || $data['user_tel']!==cookie("sms_user_tel")){
            $this->error("验证码不正确!");
         }  
        }
        $user_id=session('user.id');
        $uid=de_crypt($user_id);
        $_data=array(
             'user_tel'=>$data['user_tel'],
             'password'=>md5($data['password']),
            );
        if(M('user')->where("id=".$uid)->save($_data)!==false){
            $this->success("效验成功!开始赚钱吧!",U("/User/index"));
          }else{ 
            $this->error("处理失败,请联系管理员!");
        }
    }

    //内部检查登陆
    private function chak_login()
    {
     $SESSION=session("user");
     if(empty($SESSION['id']))
      {  
         $uid=I('get.uid')?I('get.uid'):0;
         if($uid){
           $this->redirect(U('/User/reg/'.$uid));
         } 
         $this->error("登陆超时!请重新登录!",U('/User/login'));
      }
    }

    //分享页面
    public function show()
    { 
      $id = (int)I("get.id");
      $data['uid']= I("get.uid");
      $data['news']=D("NewsView")->where(array('News.id'=>$id,'News.look'=>0))->find();
      if(!$data['news']){ 
         $this->error("文章不存在!");
      }
      $data['news']['weixin']= $data['news']['weixin']? $data['news']['weixin']:$data['news']['c_weixin'];
      $data['news']['weixin_url']= $data['news']['weixin_url']? $data['news']['weixin_url']:$data['news']['c_weixin_url'];
      //\Think\Hook::add('JiFei','User\Behaviors\JiFeiBehavior');
      import("Lib.Token.Token");
      $token= new \Token();
      $token= $token->get();
      $data['token']= $token['__hash__'];
      $data['id']=$id;
      //判断有没有关联商品，如果关联调用buy.html模板
      if($data['news']['item_id']){
          $item_id=$data['news']['item_id'];
          $data['item']= M('shop_item')->where(array('id'=>$item_id))->find();
          if(!$data['item']){
            $this->error("商品不存在.");
          }
          $tpl="buy";
      }
      $this->assign($data);
      $this->display($tpl);
    }
 



}
